﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong
{
    class Ball
    {
        Texture2D texture;
        Vector2 position;
        Rectangle size;
        Vector2 velocity;
        Vector2 maxVelocity = new Vector2(16,16);
        Random random = new Random();
        SoundEffect soundEffect;


        public Vector2 Position
        {
            get{return position;}
            set{position = value;}
                
        }

        public Rectangle Size
        {
            get { return size; }
            set { size = value; }

        }


        public Ball( GameWindow janela , Texture2D texture, Vector2 position, SoundEffect soundEffect)//<CONSTRUTOR!!!
        {
            this.soundEffect = soundEffect;
            this.texture = texture;//AQUI
            this.size = texture.Bounds;//AQUI
            reset(janela);            
            
        }

        public int Update (GameWindow janela)
        {
                  position+=velocity;//AQUI

                  return manterNaTela(janela);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);

        }

        public int VerificaColisao(Pallete palheta)
        {

            if ((position.X > palheta.position.X - size.Width) & (position.X < palheta.position.X + palheta.size.Width) ){
                if ((position.Y > palheta.position.Y - size.Height) & (position.Y < palheta.position.Y + palheta.size.Height))
                {
                    soundEffect.Play();
                    if (velocity.X > 0)
                    {
                        this.position.X = palheta.position.X - this.size.Width;
                        increaseHorizontalVelocity(true);
                    }
                    else
                    {
                        this.position.X = palheta.position.X + palheta.size.Width - this.size.X;
                        increaseHorizontalVelocity(false);
                    }
                    this.velocity.X = -this.velocity.X;

                    palheta.debugAreadeImpacto = increaseVerticalVelocity(palheta);
                  
                }
            }

            return palheta.debugAreadeImpacto;
            
        }

        private int manterNaTela(GameWindow janela)
        {
            if (position.Y <= 0)
            {
                soundEffect.Play();
                position.Y = 0;
                velocity.Y = -velocity.Y;
                
            }

            if (position.Y >= janela.ClientBounds.Height - size.Height)
            {
                soundEffect.Play();
                position.Y = janela.ClientBounds.Height - size.Height;
                velocity.Y = -velocity.Y;
                
            }

            if (position.X <= 0)
            {
                soundEffect.Play();
                position.X = 0;
                velocity.X = -velocity.X;
                return 1;
            }

            if (position.X >= janela.ClientBounds.Width - size.Width)
            {
                soundEffect.Play();
                position.X = janela.ClientBounds.Width - size.Width;
                velocity.X = -velocity.X;
                return 2;
            }
            return 0;
        }

        private int increaseVerticalVelocity(Pallete palheta)
        {
            

            double TamanhoAreaPalheta = palheta.size.Height / 15;
            int AreaDeImpacto = (int)((position.Y - palheta.position.Y) / TamanhoAreaPalheta);

            switch (AreaDeImpacto)
            {
                case 0:
                    if (velocity.Y > -maxVelocity.Y) velocity.Y -= 3;
                    break;
                case 1: case 2:
                    if (velocity.Y > -maxVelocity.Y) velocity.Y -= 2;
                    break;
                case 3: case 4: case 5:
                    if (velocity.Y > -maxVelocity.Y) velocity.Y -= 1;
                    break;
                case 6: case 7: case 8:
                    break;
                case 9: case 10: case 11:
                    if (velocity.Y < maxVelocity.Y) velocity.Y += 1;
                    break;
                case 12: case 13:
                    if (velocity.Y < maxVelocity.Y) velocity.Y += 2;
                    break;
                case 14:                    
                    if (velocity.Y < maxVelocity.Y) velocity.Y += 3;
                    break;
            }
            return AreaDeImpacto;
        }

        private void increaseHorizontalVelocity(Boolean isVelocityPositive)
        {
            if (velocity.X >= 0)
            {
                if (velocity.X < maxVelocity.X)
                {
                    velocity.X += 1;
                }
            }
            else
            {
                if (velocity.X > -maxVelocity.X) 
                {
                    velocity.X -= 1; 
                }
            }
           
        }

        public void reset(GameWindow janela)
        {
            this.position = new Vector2(
                janela.ClientBounds.Width / 2
                - this.size.Width / 2,
                janela.ClientBounds.Height / 2
                - this.size.Height / 2);//AQUI

            float randomX = (random.Next(-5, 5));
            float randomY = (random.Next(-3, 3));
            if (randomX > 0) randomX += 2;
            else randomX -= 2;
            if (randomY > 0) randomY += 2;
            else randomY -= 2;

            this.velocity = new Vector2(randomX, randomY);
        }

    }

}
